home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 15.6 KB | 545 lines |
- package symantec.itools.awt.util;
-
-
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import symantec.itools.awt.AlignStyle;
- import symantec.itools.awt.BevelStyle;
-
-
- /**
- * ProgressBar component.
- * Creates a bar that displays a percentage.
- * Commonly used to indicate the percentage completed of a
- * lengthy task.
-
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
-
- public class ProgressBar
- extends Canvas
- implements BevelStyle, AlignStyle
- {
- /**
- * Border Indent constant INDENT_ZERO.
- */
- public static final int INDENT_ZERO = 0;
-
- /**
- * Border Indent constant INDENT_ONE.
- */
- public static final int INDENT_ONE = 1;
-
- /**
- * Border Indent constant INDENT_TWO.
- */
- public static final int INDENT_TWO = 2;
-
- private int align;
- private int type;
- private int progress = 0;
- private Color color1;
- private Color color2;
- private Color textColor;
- private Color borderedColor;
- private Color progressColor;
- private boolean bShowProgress = true;
- private boolean bDrawBoxes = false;
- private int boxWidth = 10;
- private int gapWidth = 2;
-
- // declared here for faster execution
- private FontMetrics fm;
- private int xTemp;
- private int yTemp;
- private int indent = INDENT_ZERO;
-
- /**
- * Constructs a progress bar with a centered progress %, no border and zero indentation.
- */
- public ProgressBar()
- {
- this(ALIGN_CENTERED, BEVEL_NONE, INDENT_ZERO);
- }
-
- /**
- * Constructs a progress bar with the style and indentation variables specified.
- * The default parameters are the bordered color and progress % color as black, the progress bar color
- * as blue.
- * @param align progress bar alignment style
- * @param bevel progress bar bevel style
- * @param indent border indent INDENT_ZERO, INDENT_ONE, or INDENT_TWO
- */
- public ProgressBar(int align, int bevel, int indent)
- {
- // set default values
- textColor = Color.black;
- borderedColor = Color.black;
- progressColor = Color.blue;
-
- // process arguments
- setBorderIndent(indent, false);
- setBevelStyle(bevel);
- setAlignStyle(align);
- }
-
- /**
- * Set the color that the progress bar will be drawn in.
- * @param color the color that the progress bar will be drawn in.
- * @see #getProgressBarColor
- */
- public void setProgressBarColor(Color c)
- {
- progressColor = c;
- invalidate();
- }
-
- /**
- * Get the color that the progress bar will be drawing in. The default color is blue.
- * @return Color that the Progress Bar will be drawn in.
- * @see #setProgressBarColor
- */
- public Color getProgressBarColor()
- {
- return progressColor;
- }
-
- /**
- * Set the color of the percentage completed text in the progress bar.
- * @param c the color that the progress bar text will be drawn in.
- * @see #getProgressBarTextColor
- */
- public void setProgressBarTextColor(Color c)
- {
- textColor = c;
- invalidate();
- }
-
- /**
- * Get the color of the percentage completed text in the progress bar. The default color is black.
- * @return Color that the progress bar text will be drawn in.
- * @see #setProgressBarTextColor
- */
- public Color getProgressBarTextColor()
- {
- return textColor;
- }
-
- /**
- * Get the boolean that controls the drawing of the progress bar as a series of boxes.
- * The default value is false.
- * @return boolean indicating if the bar should be composed of multiple boxes.
- * @see #setDrawBoxes
- */
- public boolean getDrawBoxes()
- {
- return bDrawBoxes;
- }
-
- /**
- * Set the boolean that controls the drawing of the progress bar as a series of boxes.
- * @param b indicating if the bar should be composed of multiple boxes.
- * @see #getDrawBoxes
- */
- public void setDrawBoxes(boolean b)
- {
- bDrawBoxes = b;
- invalidate();
- }
-
- /**
- * Set the width of the boxes drawn for the progress bar.
- * @param i the pixel width of the boxes in the progress bar.
- * @see #getBoxWidth
- */
- public void setBoxWidth(int i)
- {
- boxWidth = i;
- invalidate();
- }
-
- /**
- * Get the width of the boxes drawn for the progress bar. The default value is 8 pixels.
- * @return int the pixel width of the boxes in the progress bar.
- * @see #setBoxWidth
- */
- public int getBoxWidth()
- {
- return boxWidth;
- }
-
- /**
- * Set the gap width between boxes drawn for the progress bar.
- * @param i the pixel width of the boxes in the progress bar.
- * @see #getBoxWidth
- * @see #getGapWidth
- */
- public void setGapWidth(int i)
- {
- gapWidth = i;
- invalidate();
- }
-
- /**
- * Get the gap width of the boxes drawn for the progress bar. The default value is 2 pixels.
- * @return i the pixel width of the gap between boxes in the progress bar.
- * @see #setBoxWidth
- * @see #setGapWidth
- */
- public int getGapWidth()
- {
- return gapWidth;
- }
-
- /**
- * Get the boolean that controls the display of the progress number as a percentage. The default value is true
- * @return boolean indicating if the number should be displayed.
- * @see #setShowProgress
- */
- public boolean getShowProgress()
- {
- return bShowProgress;
- }
-
- /**
- * Set the boolean that controls the display of the progress number as an percentage.
- * @param b indicating if the progress percentage should be displayed.
- * @see #getShowProgress
- */
- public void setShowProgress(boolean b)
- {
- bShowProgress = b;
- invalidate();
- }
-
- /**
- * Set the style of the progress bar's alignment.
- * @param style numeric value indicating the progress bar's alignment
- * @see #getAlignStyle
- */
- public void setAlignStyle(int style)
- {
- align = style;
- invalidate();
- }
-
- /**
- * Get the current style of the progress bar's alignment
- * @return int the style numeric value indicating progress bar's alignment
- * @see #setAlignStyle
- */
- public int getAlignStyle()
- {
- return align;
- }
-
- /**
- * Set the style of the progress bar's border.
- * @param style numeric value indicating the progress bar's style
- * @see #getBevelStyle
- */
- public void setBevelStyle(int style)
- {
- type = style;
-
- switch (type)
- {
- case BEVEL_LOWERED:
- color1 = Color.black;
- color2 = Color.white;
- break;
-
- case BEVEL_LINE:
- color1 = borderedColor;
- color2 = borderedColor;
- break;
-
- case BEVEL_RAISED:
- color1 = Color.white;
- color2 = Color.black;
- break;
-
- default: // catches any bogus type, paint(...) relies on color1 being null
- color1 = color2 = null;
- break;
- }
-
- invalidate();
- }
-
- /**
- * Get the current style of the progress bar's border
- * @return int the style numeric value indicating progress bar's border, such as BEVEL_LOWERED
- * @see #setBevelStyle
- */
- public int getBevelStyle()
- {
- return type;
- }
-
- /**
- * Set the border indent amount.
- * @param indent INDENT_ZERO, INDENT_ONE or INDENT_TWO
- * @see #getBorderIndent
- */
- public void setBorderIndent(int indent)
- {
- setBorderIndent(indent, true);
- }
-
- private void setBorderIndent(int indent, boolean bRepaint)
- {
- if (indent < INDENT_ZERO)
- this.indent = INDENT_ZERO;
- else if (indent > INDENT_TWO)
- this.indent = INDENT_TWO;
- else
- this.indent = indent;
-
- if (bRepaint)
- repaint();
- }
-
- /**
- * Get the border indent amount. The default value is INDENT_ZERO.
- * @return int INDENT_ZERO, INDENT_ONE or INDENT_TWO
- * @see #setBorderIndent
- */
- public int getBorderIndent()
- {
- return indent;
- }
-
- /**
- * Set the color for the border of BEVEL_LINE style ProgressBar. This will not be used for other styles.
- * @param color color that the border will be drawn in.
- * @see #getBorderedColor
- */
- public void setBorderedColor(Color color)
- {
- borderedColor = color;
- if (type == BEVEL_LINE)
- {
- color1 = color;
- color2 = color;
- }
- invalidate();
- }
-
- /**
- * Get the color for the border of BEVEL_LINE style ProgressBar. This will not be used for other styles.
- * @return Color that the border will be drawn in.
- * @see #setBorderedColor
- */
- public Color getBorderedColor()
- {
- return borderedColor;
- }
-
- /**
- * Set the percentage complete for the process being tracked.
- * @param p percentage complete as an integer
- */
- public void updateProgress(int p)
- {
- if (p < 0) p = 0;
- if (p > 100) p = 100;
- progress = p;
- repaint();
- }
-
- /**
- * Set the current percentage complete.
- * @param p percentage complete
- * @see #getValue
- */
- public void setValue(int p)
- {
- updateProgress(p);
- }
-
- /**
- * Get the current percentage complete.
- * @return int - current percentage complete
- * @see #setValue
- */
- public int getValue()
- {
- return progress;
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * This is a standard Java AWT method which gets called by the Java
- * AWT (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its <0,0> coordinate is this component's
- * top-left corner.
- * Typically this method paints the background color to clear the
- * component's drawing space, sets graphics context to be the foreground
- * color, and then calls paint() to draw the component.
- *
- * It is overridden here to reduce flicker by eliminating the uneeded
- * clearing of the background.
- *
- * @param g the graphics context
- * @see java.awt.Component#repaint
- * @see #paint
- */
- public void update(Graphics g)
- {
- paint(g);
- }
-
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see #update
- */
- public void paint(Graphics g)
- {
- Rectangle r = bounds();
- Color c = g.getColor();
-
- if (color1 != null)
- {
- g.clipRect(0, 0, r.width, r.height);
-
- // top
- g.setColor(color1);
- g.drawLine(1+indent, indent, r.width-3-indent, indent);
-
- // bottom
- g.setColor(color2);
- g.drawLine(1+indent, r.height-1-indent, r.width-3-indent, r.height-1-indent);
-
- // left
- g.setColor(color1);
- g.drawLine(indent, indent, indent, r.height-1-indent);
-
- // right
- g.setColor(color2);
- g.drawLine(r.width-2-indent, indent, r.width-2-indent, r.height-1-indent);
-
- g.clipRect(1+indent, 1+indent, r.width-3-indent, r.height-2-indent);
- yTemp = 1 + indent;
- }
- else
- {
- g.setColor(getBackground());
- g.drawRect(indent,indent, r.width-2-indent, r.height-1-indent);
- g.clipRect(2, 1, r.width-3, r.height-2);
- yTemp = 1;
- }
-
- //progress area and shading
- g.setColor(progressColor);
- if (bDrawBoxes)
- {
- int totalWidth = (boxWidth + gapWidth);
- int pctDrawn = boxWidth * 100 / r.width;
- int widDrawn = 0;
- int i = 0;
- while (pctDrawn < progress)
- {
- g.setColor(progressColor);
- g.fillRect((totalWidth * i), 1, boxWidth, r.height - 2);
- g.setColor(getBackground());
- g.fillRect((totalWidth * i) + boxWidth, 1, gapWidth, r.height - 2);
- widDrawn = (totalWidth * ++i);
- pctDrawn = (widDrawn + boxWidth) * 100 / r.width;
- }
- g.setColor(getBackground());
- g.fillRect(widDrawn+1, 1, r.width - widDrawn - 1, r.height - 1);
- }
- else
- {
- g.fillRect(1, 1, ((r.width) * progress) / 100, r.height - 1);
- g.setColor(getBackground());
- g.fillRect(1 + ((r.width) * progress) / 100, 1, (r.width - (((r.width) * progress)) / 100), r.height - 1);
- }
-
- // text
- if (bShowProgress)
- {
- fm = getFontMetrics(getFont());
- yTemp = ((r.height + fm.getAscent()) / 2) - 2;
-
- g.setColor(textColor);
- String sz = (Integer.toString(progress) + "%");
- xTemp = (r.width - fm.stringWidth(sz)) / 2;
- switch (align)
- {
- case ALIGN_LEFT:
- if (type == BEVEL_NONE)
- g.drawString(sz, 4, yTemp);
- else
- g.drawString(sz, 8, yTemp);
- break;
-
- case ALIGN_RIGHT:
- xTemp = r.width - fm.stringWidth(sz);
- if (type == BEVEL_NONE)
- g.drawString(sz, xTemp - 6, yTemp);
- else
- g.drawString(sz, xTemp - 10, yTemp);
- break;
-
- case ALIGN_CENTERED:
- xTemp = (r.width - fm.stringWidth(sz)) / 2;
- if (type == BEVEL_NONE)
- g.drawString(sz, xTemp, yTemp);
- else
- g.drawString(sz, xTemp, yTemp);
- break;
- }
- }
- // reset color
- g.setColor(c);
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @see #minimumSize
- */
- public Dimension preferredSize()
- {
- Dimension s = size();
- Dimension m = minimumSize();
-
- return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the minimum size of this component.
- *
- * @see #preferredSize
- */
- public Dimension minimumSize()
- {
- fm = getFontMetrics(getFont());
-
- return new Dimension(50, fm.getHeight() + 4);
- }
- }
-